#ifndef BUFFER
#define BUFFER

#include "Common.h"

constexpr uint64_t size = 1024;

class Buffer
{

public:
    Buffer();
    char *Begin();
    // 获取读写位置指针
    char *getReadOffset();
    char *getWriteOffset();
    // 获取前沿/后沿空闲空间大小
    uint64_t getPrevSpace();
    uint64_t getTailSpace();

    // 获取可写空间大小
    uint64_t ReadableSpace();
    uint64_t WriteableSpace();

    // 确保可写空间足够
    void EnsureWriteAbleSpace(uint64_t len);

    // 读/写位置后移指定长度
    void MoveWriteOffset(uint64_t offset);
    void MoveReadOffset(uint64_t offset);

    void Write(const void *data, uint64_t len);
    void WriteAndPush(const void *data, uint64_t len);
    void WriteString(const std::string &data);
    void WriteStringAndPush(const std::string &data);
    void WriteBuffer(Buffer &data);
    void WriteBufferAndPush(Buffer &data);

    void Read(void *buf, uint64_t len);
    void ReadAndPop(void *buf, uint64_t len);

    std::string ReadAsString(uint64_t len);
    std::string ReadAsStringAndPop(uint64_t len);

    // 找换行，分离出一行请求
    char *FindCRLF();

    // 获取一行请求
    std::string GetLine();
    std::string GetLineAndPop();

    void clear();

private:
    // 后续考虑换一个数据结构实现
    std::vector<char> _buffer;
    uint64_t _read_offset;  // 读偏移
    uint64_t _write_offset; // 写偏移
};

#endif
